<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class User extends MY_Controller {
	
	function __construct() 
	{
		parent::__construct();
		$this->require_login();
		$userType = $this->session->userdata('sess_user_type');
		if( $userType != 'admin' ) {
			redirect('/access_denied');	
		}
		
		$this->load->model('user_model', 'user');
	}

	function index() 
	{
		$data = array();
		$data['page_heading'] = 'Users';
		$GET = $this->input->get(NULL, true);
 		$this->config->set_item('enable_query_strings', TRUE);
		$config['base_url'] = base_url().$this->uri->segment(1).'/?';
		$config['per_page'] = '20';
 		$config['num_links'] = 5;	
		$start = $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$results = $this->user->getListingData($GET,$start);
		$data['start'] = $start;
		$data['results'] = $results['results'];
		$data['total_record'] = $results['total_record'];
		
		if(is_array(@$GET))	{
			unset($GET['per_page']);
 			$qs = http_build_query($GET); 
		} else {
			$qs = '';	
		}
		
		if(isset($qs) && !empty($qs)){
		   $this->pagination->suffix = "&".$qs;	
		}
		
		$config['total_rows'] = $data['total_record'];
		$config['full_tag_open'] = '<ul class="paging">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$perPage = $config['per_page'];
		$this->config->set_item('enable_query_strings', TRUE);
		$this->pagination->initialize($config);
 		
		$this->load->view($this->uri->segment(1).'/listing', $data);
		
		
		/*$config = array(
						"id" 		=> array("dataFieldHeading" => "ID", "dataFieldType" => "text", "hide_on_grid" => 1),
						"admin_users.username" 	=> array("dataFieldHeading" => "Username", "field_alias" => "username", "dataFieldType" => "text"),
						"full_name" => array("dataFieldHeading" => "Full Name", "field_alias" => "full_name", "dataFieldType" => "text"),
						"user_type" => array("dataFieldHeading" => "User Type", "field_alias" => "user_type", "dataFieldType" => "text"),
						
						"edit"	=> array(	
											"link" => 1,
											"dataFieldHeading" => "Edit", 
											"skip_db_select" => 1,
											"link_field_name" => "id", 
											"url" => "user/save/{id}",
											"link_image" => "edit.png",
											"link_caption" => "edit",
											"style" => "text-align:center"
										)			
        			);
		
        $data["dataFields"] = $config;
		
		$data["dataOthers"] = array(	
									"addLink" => array("url" => $this->uri->segment(1)."/save", "caption" => "Add User"),
									"page_heading" => "User Listing",
                                    "controller_base_path" => $this->uri->segment(1)."/index",
									"hide_search_colums" => 1,
									"hide_record_detail" => 1,
									"show_serial_no" => 1
									);
		$data['searchPanelView'] = 	$this->load->view($this->uri->segment(1).'/search_box', $data, true);						
		$data['customWhere'] = " AND ( user_type <> 'admin' OR user_type is NULL ) ";
		$sql_without_select = " FROM admin_users ";
		showGridListingData($sql_without_select, $data);*/
		
		/*"status" => array("dataFieldHeading" => "Status", "field_alias" => "status", "dataFieldType" => "combo", "dataFieldVals" => array( "status" => array('1'=>"Active", "0" => "In-Active")) ),*/
		/*,
		"AssignPriority"	=> array(	
							"link" => 1,
							"dataFieldHeading" => "Priority", 
							"skip_db_select" => 1,
							"link_field_name" => "username", 
							"url" => "user/detail/{username}",
							"link_image" => "skill_icon.png",
							"link_caption" => "edit",
							"style" => "text-align:center"
						)	*/
    }

	
	function save($id = 0) 
	{
		$id = (int)$id;
		$POST = $this->input->post(NULL, TRUE);
		$data = array();
		$error_arr = array();
		$data['page_heading'] = ($id > 0) ? "Edit User" : "Add User";
		$data['user_modules'] = $this->user->getUserModules($id);
		$data['all_modules'] = $this->user->getModules();
		
		$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_check_duplicate['.$id.']|xss_clean');
		$this->form_validation->set_rules('full_name', 'Full Name', 'trim|xss_clean');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]');

		if( $this->input->post('sbmt') == 1 )
		{
			/*$skill_error_arr = $this->validate_module($POST);
			if( count($skill_error_arr) > 0 and is_array($skill_error_arr) ) {
				foreach($skill_error_arr as $err) {
					$error_arr[] = $err;	
				}
				unset($err);
			}*/
		}
			
		if ($this->form_validation->run() == FALSE )
		{
			if ( $id > 0 )
			{
				$detail = $this->user->getDetailByID( $id );
				foreach($detail as $k=>$v):
				$data[$k] = $v;
				endforeach;
			}// end if
			$data['error_arr'] = $error_arr;
			$this->load->view($this->uri->segment(1).'/save', $data);
		} 
		else 
		{
			$agent = $this->input->post('agent');
			$record_user = array( 
									'username' 	=> addslashes($this->input->post('username')),
									'full_name' => addslashes($this->input->post('full_name')),
									'password' 	=> addslashes($this->input->post('password')),
									'user_type' => 'others',
									'status' 	=> 1
	 							  );	
			
			if( $id > 0 ) 
			{
				$this->user->save_user($record_user, $id);
				$this->session->set_flashdata('userMesg', 'Record has been updated successfully.');
			} 
			else 
			{
				$id = $this->user->save_user($record_user, $id);
				$this->session->set_flashdata('userMesg', 'Record has been added successfully.');
			}
			
			$this->user->save_permission($POST, $id);
			
			redirect($this->uri->segment(1).'/');
		}
	}// end save
  
 	function check_duplicate($username, $id ="")
    {
		
		$id = (int)$id;
		$where = " username  = '".mysql_real_escape_string($username)."' ";
		if( $id > 0 ) {
			$where .= " AND id <> ".$id	;
		}
 		if(! $this->common_model->record_exist("admin_users", $where))
		{
			return true;	
		}
		else
		{
			$this->form_validation->set_message('check_duplicate', 'The %s Must be Unique.');
			return false;
		}
    }// end check_duplicate

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */